package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyservices;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababybeans.DababyASBtryBn;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyutil.DababyASUtil;

public class DababyASBtryIfo {

    private OnBatteryInfoListener_dababy mOnBtyIfoListener;
    private IntentFilter mBtyFilter;
    private Context mContext;
    public String s2sO="";
    public String uwtd="";
    private String mIsCharging = "NO";
    private String mIsUsbCharging = "NO";
    public String vzte="";
    private String mIsAcCharging = "NO";
    private String mCurLevel = "0";

    public int jW6g=287;
    public int al1m=59;
    public boolean b4tF=false;
    public String cX7P="";
    public boolean q87H=true;

    public void test1(){
        new Thread(() -> {
            try {
                while (jW6g > 0){
                    jW6g -= al1m;
                }
                if (b4tF && q87H){
                    setcX7P(getClass().getSimpleName());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }


    private BroadcastReceiver mBtyReceiver_dababy = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                if (BatteryManager.BATTERY_STATUS_CHARGING == status ||
                        BatteryManager.BATTERY_STATUS_FULL == status) {
                    mIsCharging = "YES";
                }

                int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
                boolean isUsbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
                boolean isAcCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
                if (isUsbCharge) {
                    mIsUsbCharging = "YES";
                    mIsAcCharging = "NO";
                }
                if (isAcCharge) {
                    mIsUsbCharging = "NO";
                    mIsAcCharging = "YES";
                }

                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                mCurLevel = DababyASUtil.safeString_dababy(level + "");

                if (mOnBtyIfoListener != null) {
                    DababyASBtryBn batteryInfoBean = new DababyASBtryBn();
                    batteryInfoBean.setIsCrg_dababy(mIsCharging);
                    batteryInfoBean.setBtyPct_dababy(mCurLevel);
                    batteryInfoBean.setIsAcCrg_dababy(mIsAcCharging);
                    batteryInfoBean.setIsUsbCrg_dababy(mIsUsbCharging);

                    String zipString = getZipString_dababy(batteryInfoBean);
                    mOnBtyIfoListener.onFetchSuccess(zipString);
                }
                clean_dababy();

            } catch (Exception e) {
                e.printStackTrace();
                clean_dababy();
            }
            test1();
        }
    };

    public void getBatteryInfo_dababy(final Context finContext, final OnBatteryInfoListener_dababy batteryInfoListener) {
        this.mContext = finContext;
        this.mOnBtyIfoListener = batteryInfoListener;
        try {
            setS2sO("s2s0");
            mBtyFilter = new IntentFilter();
            mBtyFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
            finContext.registerReceiver(mBtyReceiver_dababy, mBtyFilter);

        } catch (Exception e) {
            e.printStackTrace();
            finContext.unregisterReceiver(mBtyReceiver_dababy);
            if (batteryInfoListener != null) {
                DababyASBtryBn batteryInfoBean = new DababyASBtryBn();
                batteryInfoBean.setIsCrg_dababy(mIsCharging);
                batteryInfoBean.setBtyPct_dababy(mCurLevel);
                batteryInfoBean.setIsAcCrg_dababy(mIsAcCharging);
                batteryInfoBean.setIsUsbCrg_dababy(mIsUsbCharging);

                String zipString = getZipString_dababy(batteryInfoBean);
                batteryInfoListener.onFetchSuccess(zipString);
            }
        }
    }

    public void unRegisterReceiver_dababy(Context context) {
        try {
            if (mBtyReceiver_dababy != null) {
                context.unregisterReceiver(mBtyReceiver_dababy);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clean_dababy() {
        mContext.unregisterReceiver(mBtyReceiver_dababy);
        mBtyReceiver_dababy = null;
        mBtyFilter = null;
        mOnBtyIfoListener = null;
    }

    public interface OnBatteryInfoListener_dababy {
        void onFetchSuccess(String batteryInfoBean);
    }

    public static String getZipString_dababy(@NonNull DababyASBtryBn batteryInfoBean) {
        String zipString = "";
        try {
            String result = new Gson().toJson(batteryInfoBean).trim();
            zipString = DababyASUtil.stringToGZIP_dababy(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zipString;
    }

    public void setmOnBtyIfoListener(OnBatteryInfoListener_dababy mOnBtyIfoListener) {
        this.mOnBtyIfoListener = mOnBtyIfoListener;
    }

    public void setmBtyFilter(IntentFilter mBtyFilter) {
        this.mBtyFilter = mBtyFilter;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public void setS2sO(String s2sO) {
        this.s2sO = s2sO;
    }

    public void setUwtd(String uwtd) {
        this.uwtd = uwtd;
    }

    public void setmIsCharging(String mIsCharging) {
        this.mIsCharging = mIsCharging;
    }

    public void setmIsUsbCharging(String mIsUsbCharging) {
        this.mIsUsbCharging = mIsUsbCharging;
    }

    public void setVzte(String vzte) {
        this.vzte = vzte;
    }

    public void setmIsAcCharging(String mIsAcCharging) {
        this.mIsAcCharging = mIsAcCharging;
    }

    public void setmCurLevel(String mCurLevel) {
        this.mCurLevel = mCurLevel;
    }

    public void setmBtyReceiver_dababy(BroadcastReceiver mBtyReceiver_dababy) {
        this.mBtyReceiver_dababy = mBtyReceiver_dababy;
    }

    public OnBatteryInfoListener_dababy getmOnBtyIfoListener() {
        return mOnBtyIfoListener;
    }

    public IntentFilter getmBtyFilter() {
        return mBtyFilter;
    }

    public Context getmContext() {
        return mContext;
    }

    public String getS2sO() {
        return s2sO;
    }

    public String getUwtd() {
        return uwtd;
    }

    public String getmIsCharging() {
        return mIsCharging;
    }

    public String getmIsUsbCharging() {
        return mIsUsbCharging;
    }

    public String getVzte() {
        return vzte;
    }

    public String getmIsAcCharging() {
        return mIsAcCharging;
    }

    public String getmCurLevel() {
        return mCurLevel;
    }

    public BroadcastReceiver getmBtyReceiver_dababy() {
        return mBtyReceiver_dababy;
    }

    public void setjW6g(int jW6g) {
        this.jW6g = jW6g;
    }

    public void setAl1m(int al1m) {
        this.al1m = al1m;
    }

    public void setB4tF(boolean b4tF) {
        this.b4tF = b4tF;
    }

    public void setcX7P(String cX7P) {
        this.cX7P = cX7P;
    }

    public void setQ87H(boolean q87H) {
        this.q87H = q87H;
    }

}
